package org.komarichin.struts2.actions;

import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;
import org.komarichin.beans.IArticle;
import org.komarichin.beans.INews;
import org.komarichin.beans.IPartition;
import org.komarichin.beans.IPost;
import org.komarichin.beans.service.IArticleService;
import org.komarichin.beans.service.IBloggerService;
import org.komarichin.beans.service.INewsService;
import org.komarichin.beans.service.IPartitionService;
import org.komarichin.struts2.beans.models.SiteMapModel;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * &nbsp;
 * </p>
 * User: Michael Komarichin
 * Date: Feb 2, 2010
 * Time: 11:48:22 PM
 */
public class SiteMapAction extends ActionSupport {
    private static final long serialVersionUID = -223977071001749970L;

    @Autowired
    private IPartitionService<Long> partitionService;
    @Autowired
    private INewsService<Long> newsService;
    @Autowired
    private IArticleService<Long> articleService;
    @Autowired
    private IBloggerService<String> bloggerDAO;

    private List<SiteMapModel> siteMap = new ArrayList<SiteMapModel>();

    private String requestDomain;

    @Override
    public String execute() throws Exception {

        HttpServletRequest req = ServletActionContext.getRequest();
        requestDomain = "http://" + req.getServerName();


        for (IPartition<Long> part : partitionService.getAll()) {
            SiteMapModel mod = new SiteMapModel();
            mod.setLastMode(part.getUpdate());
            mod.setLoc(requestDomain + "/partition/view?partition.id=" + part.getId());
            //http://localhost:8080/partition/view.action?partition.id=31
            mod.setChangefreq("monthly");
            mod.setPriority("0.3");
            siteMap.add(mod);
        }

        for (INews news : newsService.getAll()) {
            SiteMapModel mod = new SiteMapModel();
            mod.setLastMode(news.getUpdate());
            mod.setLoc(requestDomain + "/news/view?news.id=" + news.getId());
            mod.setChangefreq("monthly");
            mod.setPriority("0.8");
            siteMap.add(mod);
        }

        for (IArticle article : articleService.getAll()) {
            SiteMapModel mod = new SiteMapModel();
            mod.setLastMode(article.getUpdate());
            mod.setLoc(requestDomain + "/article/view?article.id=" + article.getId());
            mod.setPriority("0.5");
            mod.setChangefreq("monthly");
            siteMap.add(mod);
        }

        for (IPost post : bloggerDAO.getPosts()) {
            SiteMapModel mod = new SiteMapModel();
            Date tmp = post.getUpdate();
            mod.setLastMode(tmp == null ? new Date() : tmp);
            mod.setLoc(requestDomain + "/blogger/view?post.id=" + post.getId());
            mod.setPriority("0.2");
            mod.setChangefreq("monthly");
            siteMap.add(mod);
        }


        return SUCCESS;
    }

    public List<SiteMapModel> getSiteMap() {
        return siteMap;
    }

    public void setSiteMap(List<SiteMapModel> siteMap) {
        this.siteMap = siteMap;
    }

    public String getRequestDomain() {
        return requestDomain;
    }

    public void setRequestDomain(String requestDomain) {
        this.requestDomain = requestDomain;
    }
}
